﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Diagnostics;
using System.Windows.Input;
using ElisaHippo.Properties;

namespace ElisaHippo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        RecipeConfig recipeConfigForm;
        ScanningGunInput scanGunInputForm;
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected override void OnStartup(StartupEventArgs e)
        {
            log.InfoFormat("Version is： {0}", stringRes.version);
            string sMode = ConfigurationManager.AppSettings["RunMode"];

            switch (sMode)
            {
                case "p": // stringRes.posIDMode
                    StartPosID();
                    break;

                case "s":// stringRes.scanGunMode
                    StartScanGun();
                    break;

                case "m":// stringRes.mannualMode
                    StartMannual();
                    break;
            }
        }


        #region different start approach
        private void StartPosID()
        {
            AutoInput(true);
        }

        private void StartScanGun()
        {
            AutoInput(false);
        }


        private void StartMannual()
        {
            recipeConfigForm = new RecipeConfig();
            if (ConfigurationManager.AppSettings["IsConfig"] == "true")
                recipeConfigForm.Show();
            else
                recipeConfigForm.GoNextDirectly();

            recipeConfigForm.Closed += new EventHandler(recipeConfigForm_Closed);
            
        }
        #endregion

        void recipeConfigForm_Closed(object sender, EventArgs e)
        {
            beforeExit();
        }

        void beforeExit()
        {
            try
            {
                Utility.Move2WorkingFolder();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

      
        private void AutoInput(bool isPosID)
        {
            scanGunInputForm = new ScanningGunInput(isPosID);
            scanGunInputForm.Show();
            scanGunInputForm.FormClosed += new System.Windows.Forms.FormClosedEventHandler(scanGunInputForm_FormClosed);
        }

        void scanGunInputForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            beforeExit();
            Application.Current.Shutdown();
            
        }
    }
}
